package com.xingkeduo.business.order.service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.google.common.collect.Lists;
import com.xingkeduo.dto.transter.carddetail.CardDetail;
import com.xingkeduo.dto.transter.carddetail.CardDetailParameterDto;
import com.xingkeduo.dto.transter.carddetail.CardDetailResponseDto;
import com.xingkeduo.dto.transter.cardlist.CardList;
import com.xingkeduo.dto.transter.cardlist.CardListParameterDto;
import com.xingkeduo.dto.transter.cardlist.CardListResponseDto;
import com.xingkeduo.dto.transter.cardlist.Card_list;
import com.xingkeduo.dto.transter.coupondetail.CouponDetail;
import com.xingkeduo.dto.transter.coupondetail.CouponDetailParameterDto;
import com.xingkeduo.dto.transter.coupondetail.CouponDetailResponseDto;
import com.xingkeduo.dto.transter.couponlist.CouponList;
import com.xingkeduo.dto.transter.couponlist.CouponListParameterDto;
import com.xingkeduo.dto.transter.couponlist.CouponListResponseDto;
import com.xingkeduo.dto.transter.couponlist.Coupon_list;
import com.xingkeduo.dto.transter.giftbagdetail.GiftBagDetail;
import com.xingkeduo.dto.transter.giftbagdetail.GiftBagDetailParameterDto;
import com.xingkeduo.dto.transter.giftbagdetail.GiftBagDetailResponseDto;
import com.xingkeduo.dto.transter.marketingbagdetail.MarketingBagDetail;
import com.xingkeduo.dto.transter.marketingbagdetail.MarketingBagDetailParameterDto;
import com.xingkeduo.dto.transter.marketingbagdetail.MarketingBagDetailResponseDto;
import com.xingkeduo.dto.transter.marketingbagreceive.MarketingBagReceive;
import com.xingkeduo.dto.transter.marketingbagreceive.MarketingBagReceiveParameterDto;
import com.xingkeduo.dto.transter.marketingbagreceive.MarketingBagReceiveResponseDto;
import com.xingkeduo.dto.transter.marketingdetail.MarketingDetail;
import com.xingkeduo.dto.transter.marketingdetail.MarketingDetailParameterDto;
import com.xingkeduo.dto.transter.marketingdetail.MarketingDetailResponseDto;
import com.xingkeduo.dto.transter.marketingreceive.MarketingReceive;
import com.xingkeduo.dto.transter.marketingreceive.MarketingReceiveParameterDto;
import com.xingkeduo.dto.transter.marketingreceive.MarketingReceiveResponseDto;
import com.xingkeduo.dto.transter.marketingsuccessdetail.MarketingSuccessDetail;
import com.xingkeduo.dto.transter.marketingsuccessdetail.MarketingSuccessDetailParameterDto;
import com.xingkeduo.dto.transter.marketingsuccessdetail.MarketingSuccessDetailResponseDto;
import com.xingkeduo.dto.transter.marketingsuccessdetail.Marketing_success_detail;
import com.xingkeduo.dto.transter.storelist.StoreList;
import com.xingkeduo.dto.transter.storelist.StoreListParameterDto;
import com.xingkeduo.dto.transter.storelist.StoreListResponseDto;
import com.xingkeduo.dto.transter.storelist.Store_list;
import com.xingkeduo.remote.feign.MarketingFeignService;
import com.xingkeduo.util.pojo.exception.DataException;
import com.xingkeduo.utils.DateUtils;

import lombok.extern.slf4j.Slf4j;

@Service
@Slf4j
public class MarketingService {

	@Resource
	private MarketingFeignService marketingFeignService;

	public String card_list(CardListParameterDto cardParameterDto) {
		String resultJson = marketingFeignService.getUserCardList(cardParameterDto.covert());
		log.info("card_list_resultJson:{}", resultJson);
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("card_list_data:{}", data);
		JSONArray array = JSONArray.parseArray(data);
		CardList cardList = new CardList();
		List<Card_list> card_list = array.stream().map(this::queryCard_list).collect(Collectors.toList());
		cardList.setCard_list(card_list);
		CardListResponseDto responseDto = new CardListResponseDto();
		responseDto.covert(cardList);
		return JSON.toJSONString(responseDto);
	}

	private Card_list queryCard_list(Object object) {
		if (object == null) {
			return null;
		}
		Card_list card_list = new Card_list();
		card_list.covert((JSONObject) object);
		return card_list;
	}

	public String get_card_detail(CardDetailParameterDto cardDetailParameterDto) {
		String resultJson = marketingFeignService.getUserCardDetail(cardDetailParameterDto.covert());
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("get_card_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		CardDetail detail = new CardDetail();
		detail.covert(object);
		CardDetailResponseDto responseDto = new CardDetailResponseDto();
		responseDto.covert(detail);
		return JSON.toJSONString(responseDto);
	}

	public String coupon_list(CouponListParameterDto couponListParameterDto) {
		String resultJson = marketingFeignService.getUserCouponList(couponListParameterDto.covert());
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("coupon_list_data:{}", data);
		JSONArray array = JSONArray.parseArray(data);
		CouponList couponList = new CouponList();
		couponList.setInvited_count("");
		couponList.setGet_price(0);
		couponList.setCoupon_price(0);
		couponList.setShow_share(Boolean.FALSE);
		List<Coupon_list> coupon_list = array.stream().map(this::queryCoupon_list).collect(Collectors.toList());
		couponList.setCoupon_list(coupon_list);
		CouponListResponseDto responseDto = new CouponListResponseDto();
		responseDto.covert(couponList);
		return JSON.toJSONString(responseDto);
	}

	private Coupon_list queryCoupon_list(Object object) {
		if (object == null) {
			return null;
		}
		Coupon_list coupon_list = new Coupon_list();
		coupon_list.covert((JSONObject) object);
		return coupon_list;
	}

	public String coupon_detail(CouponDetailParameterDto couponDetailParameterDto) {
		String resultJson = marketingFeignService.getUserCouponDetail(couponDetailParameterDto.covert());
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("coupon_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		CouponDetail detail = new CouponDetail();
		detail.covert(object);
		CouponDetailResponseDto responseDto = new CouponDetailResponseDto();
		responseDto.covert(detail);
		return JSON.toJSONString(responseDto);
	}

	public String user_gift_bag_detail(GiftBagDetailParameterDto giftBagDetailParameterDto) {
		String resultJson = marketingFeignService.getGifBagDetail(giftBagDetailParameterDto.covert());
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("user_gift_bag_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		GiftBagDetail giftBagDetail = new GiftBagDetail();
		giftBagDetail.covert(object);
		GiftBagDetailResponseDto responseDto = new GiftBagDetailResponseDto();
		responseDto.covert(giftBagDetail);
		return JSON.toJSONString(responseDto);
	}

	private String getRrovideResultData(JSONObject jsonData) {
		String status = jsonData.getOrDefault("status", "0") == null ? "0"
				: jsonData.getOrDefault("status", "0").toString();
		if ("1".equals(status)) {
			return jsonData.getOrDefault("data", "") == null ? "" : jsonData.getOrDefault("data", "").toString();
		}
		String msg = jsonData.getOrDefault("msg", "") == null ? "" : jsonData.getOrDefault("msg", "").toString();
		throw new DataException(DataException.Type.DEFAULT, msg);
	}

	public String marketing_detail(MarketingDetailParameterDto marketingDetailParameterDto) {
		// 默认是券
		String resultJson = null;
		if (StringUtils.isBlank(marketingDetailParameterDto.getF_marketing_type())
				|| "0".equals(marketingDetailParameterDto.getF_marketing_type())) {
			resultJson = marketingFeignService.getCouponDetail(
					Long.parseLong(marketingDetailParameterDto.getF_marketing_id()),
					Long.parseLong(marketingDetailParameterDto.getF_user_id()));
		} else if ("1".equals(marketingDetailParameterDto.getF_marketing_type())) {
			resultJson = marketingFeignService.getCardDetail(
					Long.parseLong(marketingDetailParameterDto.getF_marketing_id()),
					Long.parseLong(marketingDetailParameterDto.getF_user_id()));
		}
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultDataNoThrow(jsonData);
		log.info("marketing_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		MarketingDetail detail = new MarketingDetail();
		detail.covert(object);
		detail.setTips_status(2);
		String status = jsonData.getOrDefault("status", "0") == null ? "0"
				: jsonData.getOrDefault("status", "0").toString();
		if ("0".equals(status)) {
			String msg = jsonData.getOrDefault("msg", "") == null ? "" : jsonData.getOrDefault("msg", "").toString();
			detail.setUnreason(msg);
			String code = jsonData.getOrDefault("code", "-1") == null ? "-1"
					: jsonData.getOrDefault("code", "-1").toString();
			detail.setTips_status(Integer.parseInt(code));
		}
		MarketingDetailResponseDto responseDto = new MarketingDetailResponseDto();
		responseDto.covert(detail);
		return JSON.toJSONString(responseDto);
	}

	private String getRrovideResultDataNoThrow(JSONObject jsonData) {
		return jsonData.getOrDefault("data", "") == null ? "" : jsonData.getOrDefault("data", "").toString();
	}

	public String marketing_bag_detail(MarketingBagDetailParameterDto marketingBagDetailParameterDto) {
		String resultJson = marketingFeignService.getCouponBagDetail(
				Long.parseLong(marketingBagDetailParameterDto.getF_marketing_id()),
				Long.parseLong(marketingBagDetailParameterDto.getF_user_id()));
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultDataNoThrow(jsonData);
		log.info("marketing_bag_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		MarketingBagDetail detail = new MarketingBagDetail();
		detail.covert(object);
		detail.setTips_status(2);
		String status = jsonData.getOrDefault("status", "0") == null ? "0"
				: jsonData.getOrDefault("status", "0").toString();
		if ("0".equals(status)) {
			String msg = jsonData.getOrDefault("msg", "") == null ? "" : jsonData.getOrDefault("msg", "").toString();
			detail.setUnreason(msg);
			String code = jsonData.getOrDefault("code", "-1") == null ? "-1"
					: jsonData.getOrDefault("code", "-1").toString();
			detail.setTips_status(Integer.parseInt(code));
		}
		int couponCount = 0;
		int cardCount = 0;
		List<MarketingDetail> marketingDetailList = new ArrayList<MarketingDetail>();
		JSONArray jsonArray = object.getJSONArray("marketingRelevants");
		if (jsonArray != null) {
			int size = jsonArray.size();
			for (int i = 0; i < size; i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				String marketingId = json.getOrDefault("supportBusinessId", "") == null ? ""
						: json.getOrDefault("supportBusinessId", "").toString();
				String marketingType = json.getOrDefault("marketingType", "") == null ? ""
						: json.getOrDefault("marketingType", "").toString();
				String marketingCount = json.getOrDefault("totalQuantity", "0") == null ? "0"
						: json.getOrDefault("totalQuantity", "0").toString();
				if (marketingType.equals("0")) {
					couponCount = couponCount + Integer.parseInt(marketingCount);
				}
				if (marketingType.equals("1")) {
					cardCount = cardCount + Integer.parseInt(marketingCount);
				}
				MarketingDetailParameterDto param = new MarketingDetailParameterDto();
				param.setF_marketing_id(marketingId);
				param.setF_user_id(marketingBagDetailParameterDto.getF_user_id());
				param.setF_marketing_type(marketingType);
				String result = marketing_detail(param);
				JSONObject r = JSON.parseObject(result);
				MarketingDetail marketingDetail = r.getObject("content", MarketingDetail.class);
				marketingDetail.setMarketing_count(marketingCount);
				marketingDetailList.add(marketingDetail);
			}
		}
		detail.setCard_count(cardCount + "");
		detail.setCoupon_count(couponCount + "");
		detail.setMarketing_detail_list(marketingDetailList);
		MarketingBagDetailResponseDto responseDto = new MarketingBagDetailResponseDto();
		responseDto.covert(detail);
		return JSON.toJSONString(responseDto);
	}

	public String marketing_store_list(StoreListParameterDto storeListParameterDto) {
		String resultJson = marketingFeignService
				.getMarketingStoreList(Long.parseLong(storeListParameterDto.getF_marketing_id()),Integer.parseInt(storeListParameterDto.getF_marketing_type()));
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultData(jsonData);
		log.info("marketing_store_list_data:{}", data);
		StoreList storeList = new StoreList();
		if(StringUtils.isNotBlank(data)) {
			JSONArray array = JSONArray.parseArray(data);
			List<Store_list> store_list = array.stream().map(this::queryStore_list).collect(Collectors.toList());
			storeList.setStore_list(store_list);
		}else {
			List<Store_list> store_list = new ArrayList<>();
			storeList.setStore_list(store_list);
		}
		StoreListResponseDto responseDto = new StoreListResponseDto();
		responseDto.covert(storeList);
		return JSON.toJSONString(responseDto);
	}

	private Store_list queryStore_list(Object object) {
		if (object == null) {
			return null;
		}
		Store_list store_list = new Store_list();
		store_list.covert((JSONObject) object);
		return store_list;
	}

	public String marketing_receive(MarketingReceiveParameterDto marketingReceiveParameterDto) {
		String resultJson=null;
		if(!"2".equals(marketingReceiveParameterDto.getF_marketing_type())) {
			 resultJson = marketingFeignService.bindReceiveAccount(
					Long.parseLong(marketingReceiveParameterDto.getF_user_id()),
					Long.parseLong(marketingReceiveParameterDto.getF_marketing_id()),
					Integer.parseInt(marketingReceiveParameterDto.getF_marketing_type()),
					(StringUtils.isNotBlank(marketingReceiveParameterDto.getF_receive_type()) ? Integer.parseInt(marketingReceiveParameterDto.getF_receive_type()) : null));
		}else {
			 resultJson = marketingFeignService.bindBagReceiveAccount(
					Long.parseLong(marketingReceiveParameterDto.getF_user_id()),
					Long.parseLong(marketingReceiveParameterDto.getF_marketing_id()));
		}
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultDataNoThrow(jsonData);
		log.info("marketing_receive_data:{}", data);
//		JSONObject object = JSONObject.parseObject(data);
		MarketingReceive detail = new MarketingReceive();
		detail.setTips_status(2);
//		detail.covert(object);
		String status = jsonData.getOrDefault("status", "0") == null ? "0"
				: jsonData.getOrDefault("status", "0").toString();
		if ("0".equals(status)) {
			String msg = jsonData.getOrDefault("msg", "") == null ? "" : jsonData.getOrDefault("msg", "").toString();
			detail.setUnreason(msg);
			String code = jsonData.getOrDefault("code", "-1") == null ? "-1"
					: jsonData.getOrDefault("code", "-1").toString();
			detail.setTips_status(Integer.parseInt(code));
		}
		MarketingReceiveResponseDto responseDto = new MarketingReceiveResponseDto();
		responseDto.covert(detail);
		if ("0".equals(status)) {
			responseDto.setError(1);
			responseDto.setReason(detail.getUnreason());
		}
		return JSON.toJSONString(responseDto);
	}

	public String marketing_bag_receive(MarketingBagReceiveParameterDto marketingBagReceiveParameterDto) {
		String resultJson = marketingFeignService.bindBagReceiveAccount(
				Long.parseLong(marketingBagReceiveParameterDto.getF_user_id()),
				Long.parseLong(marketingBagReceiveParameterDto.getF_marketing_id()));
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultDataNoThrow(jsonData);
		log.info("marketing_receive_data:{}", data);
//		JSONObject object = JSONObject.parseObject(data);
		MarketingBagReceive detail = new MarketingBagReceive();
		detail.setTips_status(2);
//		detail.covert(object);
		String status = jsonData.getOrDefault("status", "0") == null ? "0"
				: jsonData.getOrDefault("status", "0").toString();
		if ("0".equals(status)) {
			String msg = jsonData.getOrDefault("msg", "") == null ? "" : jsonData.getOrDefault("msg", "").toString();
			detail.setUnreason(msg);
			String code = jsonData.getOrDefault("code", "-1") == null ? "-1"
					: jsonData.getOrDefault("code", "-1").toString();
			detail.setTips_status(Integer.parseInt(code));
		}
		MarketingBagReceiveResponseDto responseDto = new MarketingBagReceiveResponseDto();
		responseDto.covert(detail);
		if ("0".equals(status)) {
			responseDto.setError(1);
			responseDto.setReason(detail.getUnreason());
		}
		return JSON.toJSONString(responseDto);
	}

	public String marketing_success_detail(MarketingSuccessDetailParameterDto marketingSuccessDetailParameterDto) {
		// 默认是券
		String resultJson = null;
		if (StringUtils.isBlank(marketingSuccessDetailParameterDto.getF_marketing_type())
				|| "0".equals(marketingSuccessDetailParameterDto.getF_marketing_type())) {
			resultJson = marketingFeignService.getCouponDetail(
					Long.parseLong(marketingSuccessDetailParameterDto.getF_marketing_id()),
					null);
		} else if ("1".equals(marketingSuccessDetailParameterDto.getF_marketing_type())) {
			resultJson = marketingFeignService.getCardDetail(
					Long.parseLong(marketingSuccessDetailParameterDto.getF_marketing_id()),
					null);
		}else if ("2".equals(marketingSuccessDetailParameterDto.getF_marketing_type())) {
			resultJson = marketingFeignService.getCouponBagDetail(
					Long.parseLong(marketingSuccessDetailParameterDto.getF_marketing_id()),
					null);
		}
		JSONObject jsonData = JSON.parseObject(resultJson);
		String data = getRrovideResultDataNoThrow(jsonData);
		log.info("marketing_success_detail_data:{}", data);
		JSONObject object = JSONObject.parseObject(data);
		MarketingSuccessDetail detail = new MarketingSuccessDetail();
		detail.covert(object);
		if (detail.getMarketing_type().equals("2")) {
			JSONArray jsonArray = object.getJSONArray("marketingRelevants");
			List<Marketing_success_detail> coupon_success_detail = new ArrayList<>();
			List<Marketing_success_detail> card_success_detail = new ArrayList<>();
			int size = jsonArray.size();
			for (int i = 0; i < size; i++) {
				JSONObject json = jsonArray.getJSONObject(i);
				String marketingType = json.getOrDefault("marketingType", "0") == null ? "0"
						: json.getOrDefault("marketingType", "0").toString();
				String businessId = json.getOrDefault("supportBusinessId", "0") == null ? "0"
						: json.getOrDefault("supportBusinessId", "0").toString();
				// 默认是券
				String result = null;
				if ("0".equals(marketingType)) {
					result = marketingFeignService.getCouponDetail(
							Long.parseLong(businessId),null);
				} else if ("1".equals(marketingType)) {
					result = marketingFeignService.getCardDetail(
							Long.parseLong(businessId),null);
				}
				JSONObject resultData = JSON.parseObject(result);
				String _data = getRrovideResultDataNoThrow(resultData);
				log.info("marketing_detail_data:{}", _data);
				JSONObject obj = JSONObject.parseObject(_data);
				if (marketingType.equals("0")) {
					Marketing_success_detail success_detail = querySuccess_coupon_detail(json);
					success_detail.setStore_id(obj.getOrDefault("storeIds", "0") == null ? "0" : obj.getOrDefault("storeIds", "0").toString());
					success_detail.setStore_name("适用部分门店");
					if("0".equals(success_detail.getStore_id())) {
						success_detail.setStore_name("适用所有门店");
					}
					//success_detail.setStore_name(obj.getOrDefault("storeIds", "0") == null ? "0" : obj.getOrDefault("storeIds", "0").toString());
					String userType = obj.getOrDefault("useType", "0") == null ? "0"
							: obj.getOrDefault("useType", "0").toString();
					if ("0".equals(userType)) {
						Integer usePeriod = obj.getOrDefault("usePeriod", "0") == null ? 0
								: Integer.parseInt(obj.getOrDefault("usePeriod", "0").toString());
						Date startDate = DateUtils.getCurdate();
						Date endDate = DateUtils.addSeconds(DateUtils.addSomeDays(startDate, usePeriod),-1);
						String use_end_time = endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT);
						success_detail.setUse_end_time(use_end_time);
					} else {
						Date endDate = obj.getDate("useEndTime");
						success_detail.setUse_end_time(endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT));
					}
					BigDecimal leastAmount = new BigDecimal(obj.getOrDefault("leastCost", "0")==null?"0":obj.getOrDefault("leastCost", "0").toString());
					leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
					success_detail.setLeast_cost(leastAmount.toString());
					success_detail.setCard_type(obj.getOrDefault("serviceType", "")==null?"":obj.getOrDefault("serviceType", "").toString());
					String couponAmount = obj.getOrDefault("couponAmount", "")==null?"":obj.getOrDefault("couponAmount", "").toString();
					if(StringUtils.isBlank(couponAmount)) {
						success_detail.setReduce_cost(Lists.newArrayList("0","0"));
					}else {
						BigDecimal amount = new BigDecimal(couponAmount);
						if("DISCOUNT".equalsIgnoreCase(success_detail.getCard_type())) {
							amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
							BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
							BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
							success_detail.setDiscount(Lists.newArrayList(one+"",two+""));
						}else{
							BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
							BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
							two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
							success_detail.setReduce_cost(Lists.newArrayList(one+"",(two+"").replaceAll("\\.", "")));
						}
					}
					coupon_success_detail.add(success_detail);
				} else {
					Marketing_success_detail success_detail = querySuccess_card_detail(json);
					success_detail.setStore_id(obj.getOrDefault("storeIds", "0") == null ? "0" : obj.getOrDefault("storeIds", "0").toString());
					success_detail.setStore_name("适用部分门店");
					if("0".equals(success_detail.getStore_id())) {
						success_detail.setStore_name("适用所有门店");
					}
					//success_detail.setStore_name(obj.getOrDefault("storeIds", "0") == null ? "0" : obj.getOrDefault("storeIds", "0").toString());
					String userType = obj.getOrDefault("useType", "0") == null ? "0"
							: obj.getOrDefault("useType", "0").toString();
					if ("0".equals(userType)) {
						Integer usePeriod = obj.getOrDefault("usePeriod", "0") == null ? 0
								: Integer.parseInt(obj.getOrDefault("usePeriod", "0").toString());
						Date startDate = DateUtils.getCurdate();
						Date endDate = DateUtils.addSeconds(DateUtils.addSomeDays(startDate, usePeriod),-1);
						String use_end_time = endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT);
						success_detail.setUse_end_time(use_end_time);
					} else {
						Date endDate = obj.getDate("useEndTime");
						success_detail.setUse_end_time(endDate == null ? null : DateUtils.formatDate(endDate, DateUtils.DATE_FORMAT));
					}
					BigDecimal leastAmount = new BigDecimal(obj.getOrDefault("leastCost", "0")==null?"0":obj.getOrDefault("leastCost", "0").toString());
					leastAmount = leastAmount.subtract(leastAmount.setScale(1,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? leastAmount : leastAmount.setScale(1, BigDecimal.ROUND_DOWN);
					success_detail.setLeast_cost(leastAmount.toString());
					success_detail.setCard_type(obj.getOrDefault("serviceType", "")==null?"":obj.getOrDefault("serviceType", "").toString());
					String couponAmount = obj.getOrDefault("couponAmount", "")==null?"":obj.getOrDefault("couponAmount", "").toString();
					if(StringUtils.isBlank(couponAmount)) {
						success_detail.setReduce_cost(Lists.newArrayList("0","0"));
					}else {
						BigDecimal amount = new BigDecimal(couponAmount);
						if("DISCOUNT".equalsIgnoreCase(success_detail.getCard_type())) {
							amount= amount.multiply(BigDecimal.valueOf(10)).setScale(1, BigDecimal.ROUND_DOWN);
							BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
							BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(0,BigDecimal.ROUND_DOWN);
							success_detail.setDiscount(Lists.newArrayList(one+"",two+""));
						}else{
							BigDecimal one = amount.setScale(0,BigDecimal.ROUND_DOWN);
							BigDecimal two =amount.subtract(one).multiply(BigDecimal.valueOf(10)).setScale(1,BigDecimal.ROUND_DOWN);
							two = two.subtract(two.setScale(0,BigDecimal.ROUND_DOWN)).compareTo(BigDecimal.valueOf(0)) > 0 ? two : two.setScale(0, BigDecimal.ROUND_DOWN);
							success_detail.setReduce_cost(Lists.newArrayList(one+"",(two+"").replaceAll("\\.", "")));
						}
					}
					card_success_detail.add(success_detail);

				}
			}
			detail.setCoupon_bag(coupon_success_detail);
			detail.setCard_bag(card_success_detail);
		}
		MarketingSuccessDetailResponseDto responseDto = new MarketingSuccessDetailResponseDto();
		responseDto.covert(detail);
		return JSON.toJSONString(responseDto);
	}

	private Marketing_success_detail querySuccess_coupon_detail(JSONObject json) {
		if (json == null) {
			return null;
		}
		Marketing_success_detail detail = new Marketing_success_detail();
		detail.covert(json);
		return detail;
	}

	private Marketing_success_detail querySuccess_card_detail(JSONObject json) {
		if (json == null) {
			return null;
		}
		Marketing_success_detail detail = new Marketing_success_detail();
		detail.covert(json);
		return detail;
	}
}
